﻿using System;
using System.Windows.Forms;

namespace CMS.Controls
{
    public class LazyTabPage : TabPage
    {
        private readonly Func<Control> _ControlFactory;
        private Lazy<Control> _Lazy;

        /// <summary>
        /// 页面是否已经创建
        /// </summary>
        public bool IsCreate => _Lazy.IsValueCreated;

        public LazyTabPage(Func<Control> controlFactory)
        {
            _ControlFactory = controlFactory;
            _Lazy = new Lazy<Control>(_ControlFactory);
        }

        /// <summary>
        /// 通知TabPage页面进行子控件加载
        /// </summary>
        public void ActiveTabPage()
        {
            if (!_Lazy.IsValueCreated)
            {
                Control control = _Lazy.Value;
                _Lazy.Value.Dock = DockStyle.Fill;
                Controls.Add(control);
            }
        }

        /// <summary>
        /// 关闭此选项卡
        /// </summary>
        public void CloseTabPage()
        {
            Controls.Clear();
            _Lazy = new Lazy<Control>(_ControlFactory);
        }

        /// <summary>
        /// 刷新TabPage
        /// </summary>
        public void ReLoadTabPage()
        {
            CloseTabPage();
            ActiveTabPage();
        }

    }

}
